//
//  LoginViewController.m
//  ExpressData
//
//  Created by Sun Thurein Aung on 19/02/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LoginViewController.h"
#import <QuartzCore/QuartzCore.h> 
#define kOFFSET_FOR_KEYBOARD 80.0
int upDistance;

@implementation LoginViewController
@synthesize singingInView, userNameTextField, passwordTextField, scrollView;
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{

    [self animateTextField: textField up: NO];
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
	int movementDistance;
	if (up){
		movementDistance = self.view.frame.origin.y - textField.frame.origin.y + upDistance; // tweak as needed
	}else {
		movementDistance = -(self.view.frame.origin.y); // tweak as needed
	}

    const float movementDuration = 0.3f; // tweak as needed


//    int movement = (up ? -movementDistance : movementDistance);
//	UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Copied to clipboard"
//													   message :[NSString stringWithFormat:@"%d  copied to clipboard.", movementDistance]
//													  delegate :nil
//											  cancelButtonTitle:@"Ok"
//											  otherButtonTitles:nil];
//	[alertView show];
//	[alertView release];

	
    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movementDistance);
    [UIView commitAnimations];
}

-(IBAction) hideKeyboard :(id)sender{
	[userNameTextField resignFirstResponder];
	[passwordTextField resignFirstResponder];
}
-(IBAction) loginPress :(id)sender{
	CGRect topRect = CGRectMake(0, 0, 1, 1);
    [scrollView scrollRectToVisible:topRect animated:YES];
	
 //   scrollView.frame = CGRectMake(0, 0, scrollviewWidth, scrollviewHeight);  
	
	singingInView.hidden = !singingInView.hidden;
	
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { 
	[theTextField resignFirstResponder]; 
	return YES; 
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"Login";
	
	
	singingInView.layer.cornerRadius = 10;
	singingInView.hidden = YES;
	userNameTextField.delegate = self;
	passwordTextField.delegate = self;
	
	UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
	if (orientation == UIInterfaceOrientationLandscapeRight ||
		orientation == UIInterfaceOrientationLandscapeLeft) {
		scrollView.contentSize = CGSizeMake(350,400);
		upDistance = 50;
	} else {
			scrollView.contentSize = CGSizeMake(350,400);
			upDistance = 100;
		}
	
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return YES;
}

-(void)willRotateToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation 
							   duration:(NSTimeInterval)duration {
	
	[super willRotateToInterfaceOrientation:toInterfaceOrientation  
								   duration:duration];
	
	if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight ||
		toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
		scrollView.contentSize = CGSizeMake(350,400);	
	upDistance = 50;
	} else {
		scrollView.contentSize = CGSizeMake(350,400);
		upDistance = 100;
	}
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
	[singingInView release];
	[userNameTextField release];
	[passwordTextField release];
	[scrollView release];
	
}


@end
